﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;

namespace RFIDStockManagement.Client
{
    public partial class SupplierEdit : BaseForm
    {
        private SupplierInfo m_SupplierInfo = null;
        private long m_Id = 0;
        public SupplierEdit(long id)
        {
            InitializeComponent();
            m_Id = id;
        }

        private void SupplierEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                BindData(m_Id);
            }
        }

        private void BindData(long id)
        {
            try
            {
                m_SupplierInfo = BasicInfoManager.GetOne(id, EnumBasicInfo.Supplier) as SupplierInfo;
                if (m_SupplierInfo == null)
                {
                    AlertForm.ShowMessage("读取不出数据，可能被删除");
                    this.Close();
                    return;
                }
                txtName.Text = m_SupplierInfo.Name;
                txtPhone.Text = m_SupplierInfo.Phone;
                txtContact.Text = m_SupplierInfo.Contact;
                txtAddress.Text = m_SupplierInfo.Address;
                //if (m_SupplierInfo.BeQuote)
                //{
                //    btnSave.Visible = false;
                //    btnCancle.Visible = false;
                //    txtName.Enabled = false;
                //    txtPhone.Enabled = false;
                //    txtContact.Enabled = false;
                //    txtAddress.Enabled = false;
                //    txtBeQuoteMessage.Visible = true;
                //    txtBeQuoteMessage.Text = m_SupplierInfo.BeQuoteMessage;
                //}
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
                this.Close();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length == 0)
                {
                    AlertForm.ShowMessage("供应商名称不能为空");
                    return;
                }
                if (m_SupplierInfo == null)
                {
                    m_SupplierInfo = new SupplierInfo();
                }
                m_SupplierInfo.Name = txtName.Text.Trim();
                m_SupplierInfo.Phone = txtPhone.Text.Trim();
                m_SupplierInfo.Contact = txtContact.Text.Trim();
                m_SupplierInfo.Address = txtAddress.Text.Trim();

                BasicInfoManager.Save(m_SupplierInfo, EnumBasicInfo.Supplier);
                AlertForm.ShowMessage("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
            
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
